@extends('admin.tableLayout')
@section('title','奖品列表')
@section('barName','奖品列表')
@php
    $config=config('admin.writing');
    $display=config('admin.isDisplay');
@endphp
@section('quote')
    @can('game')
    <button type="button" class="layui-btn layui-btn-small" id="getAll" onclick="javascript:location.href='{{route('admin.prizeSetting_add')}}'"><i class="fa fa-plus" aria-hidden="true"></i> 添加奖品</button>
     @elsecannot('game')
      @endcan
{{--    <form class="layui-form"  style="float: right">
        <div class="layui-inline">
            <input type="text" name="searchKey" placeholder="角色名称查询" autocomplete="off" value="{{$searchKey or ''}}" class="layui-input">
        </div>
        <button lay-filter="search" class="layui-btn" lay-submit><i class="fa fa-search" aria-hidden="true"></i> 查询</button>
    </form>--}}
@endsection

@section('checkbox_btn')

@endsection
@section('toolbar')

    @can('game')
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    @endcan
    @can('game')
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    @endcan
@endsection
@section('tableRender')
    <script>
        layui.use(['table','jquery','form'], function(){
            var table = layui.table;
            var $=layui.jquery;
            var form=layui.form;
            var render=table.render({
                elem:"#table",
                page:true,
                id:'idTest',
                url:'{{route('admin.prizeSetting_ajaxList')}}',
                where:{searchKey:$("input[name='searchKey']").val()
                },
                height:400,
                cols:  [[ //标题栏
                    {checkbox:true,fixed:true},
                    {field: 'id', title: 'ID', width: 80},
                    {field: 'name', title: '奖品名称', width: 200},
                    {field: 'chance', title: '奖品概率(概率之和不能超过100)，概率为整数', width: 200},
                    {field: 'created_at', title: '创建时间', width: 200},
                    {field: 'updated_at', title: '修改时间', width: 200},
                    {fixed: 'right', width:250,title:'操作', align:'center', toolbar: '#toolbar'}
                    //这里的toolbar值是模板元素的选择器
                ]],
                response: {//返回格式自定义
                    statusName: 'code' //数据状态的字段名称，默认：code
                    ,statusCode: 0 //成功的状态码，默认：0
                    ,msgName: 'msg' //状态信息的字段名称，默认：msg
                    ,countName: 'total' //数据总数的字段名称，默认：count
                    ,dataName: 'data' //数据列表的字段名称，默认：data
                }
            });
            table.on('checkbox(table)', function(obj){
                console.log(obj)
            });

            //上方
            var   active = {
                getCheckData: function(){ //获取选中数据
                    var checkStatus = table.checkStatus('idTest')
                        ,data = checkStatus.data;
                    var str=[];
                    if(data==''){
                        layer.msg('请选中所删除的行',{icon:5})
                        return false;
                    }
                    for(var i=0;i<data.length;i++){
                        str.push(data[i].id);
                    }
                    layer.confirm('是否删除所选',function(index) {
                        $.get('/admin/prizeSetting/del', {id: str}, function (obj) {
                            if (obj.info == 'success') {
                                layer.msg('删除成功', {icon: 1});
                                render.reload();
                            } else {
                                layer.msg(obj.msg, {icon: 5});
                            }

                        })
                    })

                },

            };
            $('.demoTable .layui-btn').on('click', function(){
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
            table.on('tool(table)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
                var data = obj.data; //获得当前行数据
                var layEvent = obj.event; //获得 lay-event 对应的值
                var tr = obj.tr; //获得当前行 tr 的DOM对象
                if(layEvent === 'detail'){ //查看

                } else if(layEvent === 'del'){ //删除
                    layer.confirm('真的删除行么', function(index){
                        layer.close(index);
                        //向服务端发送删除指令
                        $.get('/admin/prizeSetting/del',{id:data.id},function(obj1){
                            if(obj1.info=='success'){
                                obj.del(); //删除对应行（tr）的DOM结构，并更新缓存

                                layer.msg('删除成功',{icon:1});
                            }else{
                                layer.msg(obj1.msg);
                            }
                        });
                    });
                } else if(layEvent === 'edit'){ //编辑
                    //do something
                    //同步更新缓存对应的值
                    location.href="/admin/prizeSetting/"+data.id+"/edit";
                } else if(layEvent==='give'){//授予权限
                    location.href="/admin/role/"+data.id+"/permission";

                }
            });
        });
    </script>
@endsection

